home *** CD-ROM | disk | FTP | other *** search
- unit math;
- { This unit provides general logarithmic, trigonometric, and
- hyperbolic functions for engineering programs. Needs v4.0+
-
- Copyright 10/07/89 by
- Richard D. Van Fossan
- 3701 Ryan St. SE
- Lacey, WA 98503
-
- All rights reserved. Permission is granted for
- non-commercial use. Written permission must be
- obtained for commercial use of this package. This
- package is distributed on an as-is basis, with no
- warranty of any kind. The user takes sole responsibility
- for use of this package, including risk of loss or
- incidental damage or loss.}
- {$N+}
- interface
- type
- {$IFOPT N+}
- float = extended;
- {$ELSE}
- float = real;
- {$ENDIF}
- function fact (x:float):float; {Computes X!}
- function sgn (x:float):float; {Returns -1,0,+1 based on alg. sign}
- function rad (x:float):float; {Converts deg to radians}
- function deg (x:float):float; {Converts radians to degrees}
- function sec (x:float):float; {Computes sec(x) with x in radians}
- function csc (x:float):float;
- function tan (x:float):float;
- function cot (x:float):float;
- function arcsin (x:float):float;
- function arccos (x:float):float;
- function arccot (x:float):float;
- function arcsec (x:float):float;
- function arccsc (x:float):float;
- function arctan (x,y:float):float;
- function sinh (x:float):float; {Computes Hyperbolic sine}
- function cosh (x:float):float;
- function tanh (x:float):float;
- function csch (x:float):float;
- function sech (x:float):float;
- function coth (x:float):float;
- function arcsinh (x:float):float;
- function arccosh (x:float):float;
- function arctanh (x:float):float;
- function arccsch (x:float):float;
- function arcsech (x:float):float;
- function arccoth (x:float):float;
- function log10 (x:float):float; {Computes common log}
- function exp10 (x:float):float; {Computes common antilog}
- function log (x,y:float):float; {Computes log base y of x}
- function int_pwr (x:float;n:shortint):float; {Raises x to an integer power}
- function power (x,y:float):float; {Raises x to any power y}
- function root (x,y:float):float; {Computes yth root of x}
- function cube (x:float):float; {Computes cube of x}
- function cubrt (x:float):float; {Computes cube root of x}
- implementation
- function fact (x:float):float;
- var
- product : float;
- i,j : integer;
- begin
- i := trunc(x);
- product := 1.0;
- if (i < 0.0)
- then fact := 0.0
- else if ((i = 0.0) or (i = 1.0))
- then fact := 1.0
- else
- begin
- for j := 2 to i do product := product * j;
- fact := product;
- end;
- end;
- function sgn (x:float):float;
- begin
- if (x = 0.0)
- then sgn := 0.0
- else if (x < 0.0) then sgn := -1.0
- else sgn := 1.0;
- end;
- function rad (x:float):float;
- begin
- rad := x * pi / 180.0;
- end;
- function deg (x:float):float;
- begin
- deg := x * 180.0 / pi;
- end;
- function sec (x:float):float;
- begin
- sec := 1.0/cos(x);
- end;
- function csc (x:float):float;
- begin
- csc := 1.0/sin(x);
- end;
- function tan (x:float):float;
- begin
- tan := sin(x)/cos(x);
- end;
- function cot (x:float):float;
- begin
- cot := cos(x)/sin(x);
- end;
- function arcsin (x:float):float;
- begin
- arcsin := system.arctan(x/sqrt(1-x*x));
- end;
- function arccos (x:float):float;
- begin
- arccos := pi/2.0-arcsin(x);
- end;
- function arccot (x:float):float;
- begin
- arccot := system.arctan(1.0/x);
- end;
- function arcsec (x:float):float;
- begin
- arcsec := arccos(1.0/x);
- end;
- function arccsc (x:float):float;
- begin
- arccsc := arcsin(1.0/x);
- end;
- function arctan (x,y:float):float;
- var a : float;
- begin
- if (x=0.0)
- then
- if (y=0.0)
- then arctan := 0.0
- else arctan := pi/2.0
- else
- if (y=0.0)
- then arctan := 0.0
- else
- begin
- a := system.arctan(abs(y/x));
- if (x>0.0)
- then
- if (y>0.0)
- then arctan := a
- else arctan := -a
- else
- if (y>0.0)
- then arctan := a + pi
- end;
- end;
- function sinh (x:float):float;
- begin
- sinh := (exp(x)-exp(-x))/2.0;
- end;
- function cosh (x:float):float;
- begin
- cosh := (exp(x)+exp(-x))/2.0;
- end;
- function tanh (x:float):float;
- begin
- tanh := -exp(-x)/(exp(x)+exp(-x))*2.0+1.0;
- end;
- function csch (x:float):float;
- begin
- csch := 1.0/sinh(x);
- end;
- function sech (x:float):float;
- begin
- sech := 1.0/cosh(x);
- end;
- function coth (x:float):float;
- begin
- coth := 1.0/tanh(x);
- end;
- function arcsinh (x:float):float;
- begin
- arcsinh := ln(x+sqrt(x*x+1.0));
- end;
- function arccosh (x:float):float;
- begin
- arccosh := ln(x+sqrt(x*x-1.0));
- end;
- function arctanh (x:float):float;
- begin
- arctanh := ln((1.0+x)/(1.0-x))/2.0;
- end;
- function arccsch (x:float):float;
- begin
- arccsch := ln((sgn(x)*sqrt(x*x+1.0)+1.0)/x);
- end;
- function arcsech (x:float):float;
- begin
- arcsech := ln((sqrt(1.0-x*x)+1.0)/x);
- end;
- function arccoth (x:float):float;
- begin
- arccoth := ln((x+1.0)/(x-1.0))/2.0;
- end;
- function log10 (x:float):float;
- begin
- log10 := ln(x)/ln(10.0);
- end;
- function exp10 (x:float):float;
- begin
- exp10 := exp(x*ln(10.0));
- end;
- function log (x,y:float):float;
- begin
- log := ln(x)/ln(y);
- end;
- function int_pwr (x:float;n:shortint):float;
- var
- m : byte;
- f : float;
- begin
- m := abs(n);
- case m of
- 0 : f := 1.0;
- 1 : f := x;
- else f := x * int_pwr(x,m - 1);
- end; { case }
- if (n < 0) then int_pwr := 1.0/f
- else int_pwr := f;
- end; { Int_pwr }
- function power (x,y:float):float;
- begin
- power := exp(y*ln(abs(x)));
- end;
- function root (x,y:float):float;
- begin
- root := exp((1.0/y)*ln(abs(x)));
- end;
- function cube (x:float):float;
- begin
- cube := (x*x*x);
- end;
- function cubrt (x:float):float;
- begin
- cubrt := sgn(x)*root(abs(x),3.0);
- end;
- end.